package com.example.librarymanagement.mapper;

import com.example.librarymanagement.model.Book;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface BookMapper {
    // 获取所有图书，按名称排序
    @Select("SELECT id, book_name AS bookName, book_photo AS bookPhoto, category, author, total_stock AS totalStock, borrowed_count AS borrowedCount, description FROM books")
    List<Book> selectAllBooks();

    // 根据ID获取图书

    @Select("SELECT id, book_name AS bookName, book_photo AS bookPhoto, category, author, total_stock AS totalStock, borrowed_count AS borrowedCount, description FROM books WHERE id = #{id}")
    Book selectBookById(@Param("id") Integer id);

    /* 可以根据需要添加更多根据条件筛选图书的方法，例如： */
    @Select("<script>" +
            "SELECT * FROM books " +
            "<where>" +
            "<if test='bookName != null'> AND bookName LIKE CONCAT('%', #{bookName}, '%') </if>" +
            "<if test='author != null'> AND author LIKE CONCAT('%', #{author}, '%') </if>" +
            "<if test='category != null'> AND category = #{category} </if>" +
            "</where>" +
            "</script>")
    List<Book> selectBooksByConditions(Map<String, Object> conditions);

    // 插入新图书，并获取自动生成的ID
    @Insert("INSERT INTO books (book_name, book_photo, category, author, total_stock, borrowed_count, description) " +
            "VALUES (#{bookName}, #{bookPhoto}, #{category}, #{author}, #{totalStock}, #{borrowedCount}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    boolean insertBook(Book book);

    // 更新图书信息，不包括 borrowedCount 字段
    @Update("<script>" +
            "UPDATE books " +
            "SET book_name = #{bookName}, " +
            "    book_photo = #{bookPhoto}, " +
            "    category = #{category}, " +
            "    author = #{author}, " +
            "    total_stock = #{totalStock}, " +
            "    description = #{description} " +
            "WHERE id = #{id}" +
            "</script>")
    boolean updateBookSelective(Book book);

    // 删除图书
    @Delete("DELETE FROM books WHERE id = #{id}")
    void deleteBookById(@Param("id") Integer id);
    @Update("UPDATE books SET total_stock = total_stock - 1, borrowed_count = borrowed_count + 1 WHERE id = #{bookId}")
    void decreaseStockAndIncreaseBorrowCount(Integer bookId);


    @Update("UPDATE books SET total_stock = total_stock + 1 WHERE id = #{bookId}")
    void increaseStock(Integer bookId);

    @Select("<script>" +
            "SELECT " +
            "b.id as id, " +
            "b.book_name as bookName, " +
            "b.book_photo as bookPhoto, " +
            "b.category as category, " +
            "b.author as author, " +
            "b.total_stock as totalStock, " +
            "b.borrowed_count as borrowedCount, " +
            "b.description as description " +
            "FROM books b " +
            "<where>" +
            "<if test='bookName != null'> b.book_name LIKE CONCAT('%', #{bookName}, '%') </if> " +
            "<if test='author != null'> AND b.author LIKE CONCAT('%', #{author}, '%') </if> " +
            "<if test='category != null'> AND b.category LIKE CONCAT('%', #{category}, '%') </if> " +
            "</where>" +
            "ORDER BY b.borrowed_count DESC " +
            "</script>")
    List<Book> searchBooksByCriteria(Map<String, String> searchCriteria);
    @Select("<script>" +
            "SELECT " +
            "b.id as id, " +
            "b.book_name as bookName, " +
            "b.book_photo as bookPhoto, " +
            "b.category as category, " +
            "b.author as author, " +
            "b.total_stock as totalStock, " +
            "b.borrowed_count as borrowedCount, " +
            "b.description as description " +
            "FROM books b " +
            "ORDER BY b.borrowed_count DESC, b.book_name ASC" + // 先按借阅次数降序，再按书名升序
            "</script>")
    List<Book> selectBorrowRankBooks();
}